package com.xmx.productcenter.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.xmx.productcenter.entity.BrandEntity;
import com.xmx.productcenter.service.BrandService;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 品牌表
 *
 * @author fbb
 * @email boyinbing@126.com
 * @date 2021-08-11 01:24:56
 */
@Api(tags = "BrandController", description = "品牌表服务")
@RestController
@RequestMapping("productcenter/brand")

public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("productcenter:brand:list")
    @ApiOperation("获取所有品牌表信息")
    public R list(@RequestParam Map<String, Object> params){
        System.out.println(params);

        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("productcenter:brand:info")
    @ApiOperation("获取指定id的品牌表详情")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加品牌表信息")
    //@RequiresPermissions("productcenter:brand:save")
    public R save(@RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id品牌表信息")
    //@RequiresPermissions("productcenter:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id品牌表信息")
    //@RequiresPermissions("productcenter:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getlist")
    public R getlist(Integer page,Integer limit){
        QueryWrapper<BrandEntity> queryWrapper  = new QueryWrapper<>();
        queryWrapper.lambda().between(BrandEntity::getId,1001000,1015000);
        Page<BrandEntity> sort_order = brandService.page(new Page<BrandEntity>(page, limit).addOrder(OrderItem.desc("sort_order")),queryWrapper);
        Map pages = new HashMap();
        pages.put("list",sort_order.getRecords());
        pages.put("total",sort_order.getTotal());
        pages.put("page",page);
        pages.put("pages",sort_order.getPages());
        pages.put("limit",limit);
        return R.ok().put("data",pages);
    }


}
